/**************************************************************************************
*  Component of benchmarking program.
**************************************************************************************
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY,  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL RTJ COMPUTING BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
**************************************************************************************/
public abstract class BenchmarkUnit implements Runnable {

    private int loopCount, memErrorCount;
    String name;
    private boolean runBenchmark;

    public BenchmarkUnit() {
        loopCount = 0;
        memErrorCount = 0;
        runBenchmark = true;
        name = null;
    }

    public int getLoopCount() {
        return loopCount;
    }

    public int getOutOfMemoryErrorCount() {
        return memErrorCount;
    }

    public final void run() {
        while (runBenchmark) {
            try {
                execute();
                loopCount++;
            } catch (OutOfMemoryError oomErr) {
                memErrorCount++;
                reset();
            }
        }
    }

    public String getName() {
        return name;
    }

    public void stopRunning() {
        runBenchmark = false;
    }

    void reset() {};

    abstract int execute();

}

